/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.PanelUI;

/**
 *
 * @author paulocanedo
 */
public class LoadingComponent {

    private JPanel panel;

    public LoadingComponent() {
        panel = new LoadingPanel();

        init();
    }

    public LoadingComponent(int amount) {
        panel = new LoadingPanel(amount);

        init();
    }

    private void init() {
        panel.setUI(new TranslucentPanelUI());
    }

    public JComponent getComponent() {
        return panel;
    }

    public static void main(String... args) {
        LoadingComponent loading = new LoadingComponent();
        JComponent component = loading.getComponent();

        JFrame frame = new JFrame();
        frame.setBounds(0, 20, 500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//        frame.add(component);
        frame.setGlassPane(component);
        component.setVisible(true);
        frame.setVisible(true);
    }
}

class LoadingPanel extends JPanel {

    private int position;
    private int amount;
    private int angle;
    private ActionListener alistener = new ActionListener()              {

        @Override
        public void actionPerformed(ActionEvent e) {
            position++;
            if (position >= amount) {
                position = 0;
            }
            repaint();
        }
    };
    private Timer timer = new Timer(70, alistener);

    public LoadingPanel() {
        this(18);
    }

    public LoadingPanel(int amount) {
        this.amount = amount;

        position = 0;
        angle = 360 / amount;

        init();
    }

    private void init() {
        timer.setRepeats(true);

        setOpaque(false);
        setDoubleBuffered(true);
        timer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int middleX = getWidth() / 2, middleY = getHeight() / 2;
        int w = 50, x = getWidth() / 2 + w / 2, y = middleY,
                h = w / 6, corner = h;

        y = y - h / 2;

        g2.rotate(Math.toRadians(angle * position), middleX, middleY);
        for (int i = 0; i < amount; i++) {
            float opacity = (amount - i) / (float) amount;

            Composite composite = AlphaComposite.SrcOver.derive(opacity);
            g2.setComposite(composite);
            g2.fillRoundRect(x, y, w, h, corner, corner);
            g2.rotate(Math.toRadians(angle), middleX, middleY);
        }

        g2.dispose();
    }
}

class TranslucentPanelUI extends PanelUI {

    private Composite composite = AlphaComposite.SrcOver.derive(0.4f);
    private Color color = new Color(0x99bbcf);
    private static ComponentUI componentUI;

    public static ComponentUI createUI() {
        if (componentUI == null) {
            componentUI = new TranslucentPanelUI();
        }
        return componentUI;
    }

    public TranslucentPanelUI() {
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setComposite(composite);

        g2.setColor(color);
        g2.fillRect(0, 0, c.getWidth(), c.getHeight());

        g2.dispose();
    }
}